<template>
    <div>
        <div class="head-container" ref="hMain">
            <el-input v-model="deptName" placeholder="请输入名称" clearable prefix-icon="el-icon-search"
                style="margin-bottom: 20px" />
        </div>
       

        <div class="head-container" :style="{ height: tableHeight + 'px' }" style="overflow: auto"> <!--其余展开三层节点-->
            
            <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
                :filter-node-method="filterNode" ref="tree" node-key="id" highlight-current @node-click="handleNodeClick"
                v-if="isnoClick" :style="size" :default-expanded-keys="keylist"/>

            <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false" v-else
                :filter-node-method="filterNode" ref="tree" node-key="id" highlight-current :style="size"
                :default-expanded-keys="keylist" />
            <!-- el tree树结构默认展开3层:default-expanded-keys="[deptOptions[0].id, deptOptions[0].children[0].id] -->
        </div>
    </div>
</template>

<script>
// import Vue from "vue";
// import VThrottle from "v-throttle";

// Vue.use(VThrottle, {
//   // 指令名称，可选，默认为 `v-throttle`
//   name: "v-throttle",
//   // 全局延时，可选，默认为 1000
//   time: 3000,
//   // 是否展示调试相关的信息，默认为false，不展示。注意：报错信息不会被关闭
//   dev: true,
// });

import {getTreesList } from "@/api/cqbc/all.js"
import estimateTableHeight from "@/mixins/estimateTableHeight";
export default {
    mixins: [estimateTableHeight], // 混入方法
    data() {
        return {
            noClick: true,
            // 部门名称
            deptName: undefined,
            // 部门树选项
            deptOptions: undefined,
            defaultProps: {
                children: "children",
                label: "label"
            },
            parentCategoryIdList: [], //判断categoryId
            parentPropertyNameList: [], //nodeType=c 时  才会有数据
            parentPropertyValueList: [],//nodeType=c 时  才会有数据
            parentFondsid: [],
            parentnodeType: [],
            // 查询参数
            queryParams: {
                fondsId: undefined,
                categoryId: undefined,
                sub_table_name: undefined,
                menuId: undefined,
                propertyName: undefined,
                propertyValue: undefined,
                label: undefined,
                nodeType: undefined

            },
            size: "",
            keylist: []
        }
    },
    watch: {
        // 根据名称筛选部门树
        deptName(val) {
            this.$refs.tree.filter(val);
        },
        isRefresh: {
            handler(newVal) {
                console.log(this.$store.getters.size, "222")
                if (this.$store.getters.size == 'small') {
                    this.size = 'font-size: 12px;'
                } else {
                    this.size = 'font-size: 14px;'
                }
                if (newVal == true) {
                    this.getTreePost();

                }
            },
            immediate: true
        }
    },
    //source==1 档案树调用   
    //isnoClick  ////防止树重复点击
    props: ["isRefresh", 'isnoClick','menuId'],
    created() {
        this.getTreePost();


    },
    computed: {

        isSize() {

            if (this.$store.getters.size == 'small') {
                this.size = 'font-size: 12px;'
            } else {
                this.size = 'font-size: 14px;'
            }
            return this.size
        }


    },
    methods: {


        /** 查询部门下拉树结构 */
        getTreePost() {
          
                var defaultExpandedKeys = [];
                getTreesList().then(response => {
                    this.deptOptions = response.data[0].children;
                    if (this.deptOptions[0].children != undefined) {
                        if (this.deptOptions.length > 0) {
                            for (var i in this.deptOptions) {
                                defaultExpandedKeys.push(this.deptOptions[i].id) //设置默认展开节点
                                if (this.deptOptions[i].children != undefined) {
                                for (var j in this.deptOptions[i].children) {
                                    defaultExpandedKeys.push(this.deptOptions[i].children[j].id)
                                }
                            }
                            }
                        }
                        this.keylist = defaultExpandedKeys
                    } else {
                        this.keylist = [this.deptOptions[0].id]
                    }
                });
            
        },
        // 筛选节点
        filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },

        //判断获取categoryId
        getParent(node) {
            // 判断当前节点是否有父节点，并且父节点上的data不能是数组
            if (node.parent && !Array.isArray(node.parent.data)) {
                if (node.parent.data.categoryId != null) {
                    // 将父节点上data的menuid存储在 `parentlist` 里
                    node.parent.data instanceof Object && this.parentCategoryIdList.push(node.parent.data.categoryId)
                }
                // // 递归调用 父节点上有父节点
                this.getParent(node.parent)
                // 当父节点上data是数组时点击对象为当前tree最高目录 并且parentList必须是0
            } else if (this.parentCategoryIdList.length === 0) {
                // 存储当前节点的id到parentList
                this.parentCategoryIdList.push(node.data.categoryId)
            }
        },





        //判断获取fondsid
        getfondsId(node) {
            // 判断当前节点是否有父节点，并且父节点上的data不能是数组
            if (node.parent && !Array.isArray(node.parent.data)) {
                if (node.parent.data.fondsId != null) {
                    // 将父节点上data的menuid存储在 `parentlist` 里
                    node.parent.data instanceof Object && this.parentFondsid.push(node.parent.data.fondsId)
                }
                // // 递归调用 父节点上有父节点
                this.getfondsId(node.parent)
                // 当父节点上data是数组时点击对象为当前tree最高目录 并且parentList必须是0
            } else if (this.parentFondsid.length === 0) {
                // 存储当前节点的id到parentList
                this.parentFondsid = undefined;
            }
        },

        //判断获取nodeType
        getNodeType(node) {
            // 判断当前节点是否有父节点，并且父节点上的data不能是数组
            if (node.parent && !Array.isArray(node.parent.data)) {
                if (node.parent.data.nodeType != "n" && node.parent.data.nodeType != null) {
                    // console.log(node.parent.data.nodeType,"kdsljfkldsj=+++++++++++++++++++++++++")
                    // 将父节点上data的menuid存储在 `parentlist` 里
                    node.parent.data instanceof Object && this.parentnodeType.push(node.parent.data.nodeType)
                }
                // // 递归调用 父节点上有父节点
                this.getNodeType(node.parent)
                // 当父节点上data是数组时点击对象为当前tree最高目录 并且parentList必须是0
            } else if (this.parentnodeType.length === 0) {
                // 存储当前节点的id到parentList
                this.parentnodeType = undefined;
            }
        },

        //判断获取propertyName和node.parent.data.propertyValue
        getParentPName(node) {
            // 判断当前节点是否有父节点，并且父节点上的data不能是数组
            if (node.parent && !Array.isArray(node.parent.data)) {

                if (node.parent.data.propertyName != null) {
                    // 将父节点上data的menuid存储在 `parentlist` 里
                    node.parent.data instanceof Object && this.parentPropertyNameList.push(node.parent.data.propertyName);
                    node.parent.data instanceof Object && this.parentPropertyValueList.push(node.parent.data.propertyValue)
                }
                // 递归调用 父节点上有父节点
                this.getParentPName(node.parent)
                // 当父节点上data是数组时点击对象为当前tree最高目录 并且parentList必须是0
            } else if (this.parentPropertyNameList.length === 0) {
                // 存储当前节点的id到parentList
                this.parentPropertyNameList = [];
                this.parentPropertyValueList = [];
            }
        },


        //判断获取propertyName和node.parent.data.propertyValue
        getsubTableNamePName(node) {
            // 判断当前节点是否有父节点，并且父节点上的data不能是数组
            if (node.parent && !Array.isArray(node.parent.data)) {
                if (node.parent.data.subTableName != null) {
                    // 将父节点上data的menuid存储在 `parentlist` 里
                    node.parent.data instanceof Object && this.parentsubTableNameList.push(node.parent.data.subTableName);
                }
                // 递归调用 父节点上有父节点
                this.getsubTableNamePName(node.parent)
                // 当父节点上data是数组时点击对象为当前tree最高目录 并且parentList必须是0
            } else if (this.parentsubTableNameList.length === 0) {
                // 存储当前节点的id到parentList
                // this.parentsubTableNameList = [];

            }
        },

        // 节点单击事件
        handleNodeClick(data, node) {
            // console.log(data,node)

            let that = this;
            if (that.noClick) {
                that.noClick = false;
                this.parentCategoryIdList = [];
                this.parentPropertyNameList = [];
                this.parentPropertyValueList = [];
                this.parentsubTableNameList = [];
                this.parentFondsid = [];
                this.parentnodeType = [];
                this.getParent(node);
                this.getParentPName(node);
                this.getfondsId(node);
                this.getNodeType(node);
                this.getsubTableNamePName(node);
                if (data.subTableName == null) {
                    this.queryParams.sub_table_name = this.parentsubTableNameList[0];  //nodeType=l 才返回subTableName数据字段
                } else {
                    this.queryParams.sub_table_name = data.subTableName
                }
                //判断当前节点categoryId是否有数据 如果是null,就找父节点的categoryId
                if (data.categoryId == null) {
                    this.queryParams.categoryId = this.parentCategoryIdList[0]

                } else {
                    //  若当前点击的子节点data.categoryId字段有数据，就直接用当前的categoryId
                    this.queryParams.categoryId = data.categoryId;
                }


                this.queryParams.parentNodeType = node.parent.data.nodeType;

                //  this.queryParams.categoryIdType = node.parent.data.categoryId;
                //判断当前节点的nodeType是否==n,如果是N获取它的上一级直到nodeType不是==n的节点
                if (data.nodeType != "n") {
                    this.queryParams.nodeType = data.nodeType;
                } else {
                    this.queryParams.nodeType = this.parentnodeType[0]
                }

                //判断parentFondsid
                if (this.parentFondsid != undefined) {
                    this.queryParams.fondsId = this.parentFondsid[0]
                } else {
                    this.queryParams.fondsId = data.fondsId;
                }
                if (data.propertyName == null) {
                    this.queryParams.propertyName = this.parentPropertyNameList
                    this.queryParams.propertyValue = this.parentPropertyValueList;
                } else {
                    var arrName = this.parentPropertyNameList;
                    arrName.push(data.propertyName);
                    this.queryParams.propertyName = arrName;

                    var arrValue = this.parentPropertyValueList;
                    arrValue.push(data.propertyValue);
                    this.queryParams.propertyValue = arrValue;
                }
                this.queryParams.menuId = this.$route.meta.menuId;

                this.queryParams.id = data.id;
                this.queryParams.label = data.label;

                this.$emit("handleNodeClick", this.queryParams)
                setTimeout(function () {
                    that.noClick = true;
                }, 1000)    //三秒内不能再次点击
            } else {

                this.$message({ message: '重复点击，请稍后再试!', type: 'warning' })
            }

        },
    }
}
</script>
<style lang="scss" scoped>
::v-deep .el-tree-node__label {
    font-size: inherit;
}
</style>
